import 'package:flutter/material.dart';

//使用：
//1)使用主色
//Text(
//  '测试',
//  style: TextStyle(color: SkinConfig().mainColor()),
//)
//2)切换主题颜色
//SkinConfig().switchSkin();

//色彩类型
const String skinStyleDay = 'day';
const String skinStyleNight = 'night';

class SkinConfig{
  factory SkinConfig() =>_getInstance();
  static SkinConfig get instance => _getInstance();
  static SkinConfig _instance;

  //颜色配置列表
  static List skinStyleList = [
    skinStyleDay,
    skinStyleNight
  ];

  //配色类型标记
  //默认配置:白天
  static String _skinStyle = skinStyleDay;

  //主色
  Color mainColor(){
    Color color ;
    switch (_skinStyle) {
      case skinStyleDay : color = Color(0xFF00549D);
      break;
      case skinStyleNight : color = Color(0xFF333333);
      break;
    }
    return color;
  }

  //主背景色
  Color mainBGColor(){
    Color color ;
    switch (_skinStyle) {
      case skinStyleDay : color = Color(0xFFF8F8F8);
      break;
      case skinStyleNight : color = Color(0xFF191C29);
      break;
    }
    return color;
  }

  //切换色彩配置
  void switchSkin(){
    //轮换配色
    int currentIndex = skinStyleList.indexOf(_skinStyle);
    int destIndex = 0;
    if(currentIndex < skinStyleList.length - 1){
      destIndex = currentIndex + 1;
    }
    _skinStyle = skinStyleList.elementAt(destIndex);
    changeSkinCallBack();
  }

  //色彩变动时候需要被调用的方法
  List<Function> changeReceiver = new List();

  //注册回调
  void regChangeAction(Function function()){
    changeReceiver.add(function);
  }

  //反注册回调
  void unRegChangeAction(Function function()){
    changeReceiver.remove(function);
  }

  //调用回调
  void changeSkinCallBack(){
    for(int i = 0 ; i < changeReceiver.length;i++){
      Function function = changeReceiver[i];
      function();
    }
  }

  SkinConfig._internal() {
    // 初始化
  }

  static SkinConfig _getInstance() {
    if (_instance == null) {
      _instance = new SkinConfig._internal();

    }
    return _instance;
  }
}